package com.example.redstarcommerce.cn.dh.history.controller;

import com.example.redstarcommerce.cn.dh.common.vo.Result;
import com.example.redstarcommerce.cn.dh.history.pojo.History;
import com.example.redstarcommerce.cn.dh.history.service.impl.HistoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName HistoryController
 * @Author AndyWu
 * @Date 2024-11-01 22:35
 * @Motto 学不死就往死里学
 * @Version 1.0
 */
@RestController
@RequestMapping("/history")
@Api(tags = "历史订单",description = "订单")
@Slf4j
public class HistoryController {

    @Autowired
    private HistoryServiceImpl historyService;;


    @RequestMapping("/insert")
    @ApiOperation(value = "历史订单添加",notes = "insertHistory",httpMethod = "POST")
    public Object insertHistory( String name, String type, String introduce, Integer scoernumber) {
        UUID uuid = UUID.randomUUID();
        String uid = uuid.toString();
        String map = "所有地区可用";
        //减少一天
        Timestamp begintime = new Timestamp(System.currentTimeMillis());
        begintime.setTime(begintime.getTime() - 24 * 60 * 60 * 1000);
        Timestamp endtime = new Timestamp(System.currentTimeMillis());
        Result<Boolean> booleanResult = historyService.insertHistory( uid, name, type, introduce, scoernumber, begintime, endtime, map);
        log.info("insertHistory"+booleanResult);
        return booleanResult;
    }

    @RequestMapping("getall")
    @ApiOperation(value = "历史订单查询",notes = "getAllHistory",httpMethod = "GET")
    public Object getAllHistory() {
        Result<List<History>> booleanResult = historyService.getAllHistory();
        log.info("getAllHistory"+booleanResult);
        return booleanResult;
    }
    @RequestMapping("/update")
    @ApiOperation(value = "历史订单修改", notes = "updateHistory", httpMethod = "POST")
    public Object updateHistory(String uid, String name, String type, String introduce, Integer scoernumber, String begintime, String endtime) {
        log.info("updateHistory");

        // 预处理日期字符串
        begintime = preprocessDateString(begintime);
        endtime = preprocessDateString(endtime);

        // 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        dateFormat.setLenient(false); // 不允许宽松解析

        try {
            // 将字符串转换为Date对象
            Date beginDate = dateFormat.parse(begintime);
            Date endDate = dateFormat.parse(endtime);

            // 将Date对象转换为Timestamp
            Timestamp begintime1 = new Timestamp(beginDate.getTime());
            Timestamp endtime1 = new Timestamp(endDate.getTime());

            Result<Boolean> booleanResult = historyService.updateHistory(uid, name, type, introduce, scoernumber, begintime1, endtime1);
            log.info("updateHistory" + booleanResult);

            if (booleanResult.getCode() == 200) {
                return Result.success(true);
            }
            return booleanResult;
        } catch (ParseException e) {
            // 捕获并处理日期解析异常
            log.error("Invalid date format: " + e.getMessage());
            return Result.fail("日期格式无效，请使用 yyyy-MM-dd'T'HH:mm:ss.SSS Z 格式");
        }
    }

    private String preprocessDateString(String dateString) {
        // 将 "00:00" 替换为 "GMT+00:00"
        if (dateString.endsWith(" 00:00")) {
            dateString = dateString.replace(" 00:00", " GMT+00:00");
        }
        return dateString;
    }





    @RequestMapping("/delete")
    @ApiOperation(value = "历史订单删除",notes = "deleteHistory",httpMethod = "POST")
    public Object deleteHistory(String uid) {
        log.info( "deleteHistory");
        Result<Boolean> booleanResult = historyService.deleteHistory(uid);
        log.info("deleteHistory"+booleanResult);
        if (booleanResult.getCode() == 200){
            return Result.success(true);
        }
        return booleanResult;
    }



}
